package com.a0soft.gphone.aTruffleHog.wnd.info;
/*
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 3, or (at your option) any
 * later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * Copyright(C) 2011 
 *   Alger Lin <addre75 (at) gmail.com>
 *
 */

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;
import com.a0soft.gphone.aTruffleHog.R;
import com.a0soft.gphone.aTruffleHog.di.DeviceInfo;
import com.a0soft.gphone.aTruffleHog.di.WifiInfo;
import com.a0soft.gphone.aTruffleHog.util.CONSTS;
import com.a0soft.gphone.aTruffleHog.util.aDebug;

/**Agent to handle view for wifi information.*/
public class InfoWFView {
    /**Tag for log.*/
    private static final String LOG_TAG = InfoWFView.class.getSimpleName();
    /**Symbol before data.*/
    private static final String SYMB_PREFIX = ": ";
    /**Symbol for new line.*/
    private static final String SYMB_NEWLINE = "\n";
    /**Application environment.*/
    private Context             m_oContext = null;
    
    /**Constructor.
     * @param oContext  [in] Application environment.
     * @param oInfo     [in] Device information.*/
    public
    InfoWFView(Context cxt, DeviceInfo di)
    {
        m_oContext = cxt;
    }
    
    /**Get view for sensor information.
     * @param for_preview   [in] Flag to indicates preview status.
     * @return              Return view for sensor information.*/
    public View
    getView(boolean for_preview)
    {
        LinearLayout                        returnObj = null;
        ArrayList<HashMap<String, String>>  listConf = null;
        String                              szData = null;
        boolean                             bData = false;
        //Initiate view
        returnObj = new LinearLayout(m_oContext);
        returnObj.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
        returnObj.setOrientation(LinearLayout.VERTICAL);
        //Put wifi capability information into view
        if(((szData = translatesData2Str(WifiInfo.getWifiData(m_oContext, WifiInfo.GROUP_CAPABILITY, for_preview))) != null) && (szData.length() > 0)) {
            View                oView = LayoutInflater.from(m_oContext).inflate(R.layout.info_item, null);
            InfoItemViewWrapper oWrap = new InfoItemViewWrapper(oView);
            oView.setTag(oWrap);
            oWrap.setItemName(WifiInfo.getWifiDataGroupName(WifiInfo.GROUP_CAPABILITY));
            returnObj.addView(oView);
            oWrap.setItemDetail(szData);
            bData = true;
        }
        //Put wifi connection information into view
        if(!for_preview && ((szData = translatesData2Str(WifiInfo.getWifiData(m_oContext, WifiInfo.GROP_CONNECT, false))) != null) && (szData.length() > 0)) {
            View                oView = LayoutInflater.from(m_oContext).inflate(R.layout.info_item, null);
            InfoItemViewWrapper oWrap = new InfoItemViewWrapper(oView);
            oView.setTag(oWrap);
            oWrap.setItemName(WifiInfo.getWifiDataGroupName(WifiInfo.GROP_CONNECT));
            returnObj.addView(oView);
            oWrap.setItemDetail(szData);
            bData = true;
        }
        //Put DHCP information into view
        if(!for_preview && ((szData = translatesData2Str(WifiInfo.getWifiData(m_oContext, WifiInfo.GROP_DHCP, false))) != null) && (szData.length() > 0)) {
            View                oView = LayoutInflater.from(m_oContext).inflate(R.layout.info_item, null);
            InfoItemViewWrapper oWrap = new InfoItemViewWrapper(oView);
            oView.setTag(oWrap);
            oWrap.setItemName(WifiInfo.getWifiDataGroupName(WifiInfo.GROP_DHCP));
            returnObj.addView(oView);
            oWrap.setItemDetail(szData);
            bData = true;
        }
        //Put wifi hotspot information into view
        if(!for_preview && ((szData = translatesData2Str(WifiInfo.getWifiData(m_oContext, WifiInfo.GROUP_HOTSPOT, false))) != null) && (szData.length() > 0)) {
            View                oView = LayoutInflater.from(m_oContext).inflate(R.layout.info_item, null);
            InfoItemViewWrapper oWrap = new InfoItemViewWrapper(oView);
            oView.setTag(oWrap);
            oWrap.setItemName(WifiInfo.getWifiDataGroupName(WifiInfo.GROUP_HOTSPOT));
            returnObj.addView(oView);
            oWrap.setItemDetail(szData);
            bData = true;
        }
        //Put wifi configurations into view
        if(!for_preview && ((listConf = WifiInfo.getWifiConfData(m_oContext)) != null) && !listConf.isEmpty()) {
            Iterator<HashMap<String, String>>   oItr = listConf.iterator();
            //Put configuration one by one
            while(oItr.hasNext()) {
                HashMap<String, String> oData = oItr.next();
                if(((szData = translatesData2Str(oData)) != null) && (szData.length() > 0)) {
                    View                oView = LayoutInflater.from(m_oContext).inflate(R.layout.info_item, null);
                    InfoItemViewWrapper oWrap = new InfoItemViewWrapper(oView);
                    oView.setTag(oWrap);
                    oWrap.setItemName(WifiInfo.getWifiConfName(oData));
                    returnObj.addView(oView);
                    oWrap.setItemDetail(szData);
                    bData = true;
                }
            }
        }
        //Show message if without valid data
        if(!bData) {
            TextView    oTxt = new TextView(m_oContext);
            oTxt.setText(CONSTS.UNAVAILABLE);
            returnObj.addView(oTxt, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
        }
        return returnObj;
    }
    
    /**Translates data set into String.
     * @param oData [in] Data set.
     * @return      Return String of data if succeeded, return null if failed.*/
    private static String translatesData2Str(HashMap<String, String> oData)
    {
        StringBuffer    returnObj = null;
        if(oData == null) {
            if(aDebug.ENABLED)
                aDebug.w(LOG_TAG, "Data is null");
        }
        else if(oData.isEmpty()) {
            returnObj = new StringBuffer();
            if(aDebug.ENABLED)
                aDebug.d(LOG_TAG, "Data is empty");
        }
        //Translates data set into string buffer
        else {
            String  szKeys[] = WifiInfo.getSortedKeyOfWifiData(oData);
            if(szKeys != null) {
                returnObj = new StringBuffer();
                //Handle data in set one by one
                for(String szKey: szKeys) {
                    String  szVal = oData.get(szKey);
                    //Put data name and value into string buffer
                    if((szKey != null) && (szKey.length() > 0) && (szVal != null) && (szVal.length() > 0)) {
                        if(returnObj.length() > 0)
                            returnObj.append(SYMB_NEWLINE);
                        returnObj.append(szKey);
                        returnObj.append(SYMB_PREFIX);
                        returnObj.append(szVal);
                    }
                }
            }
        }
        return ((returnObj != null)? returnObj.toString(): null);
    }
}
